package com.ruoyi.web.controller.airplane;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.dto.AirPlaneAndDeviceDetailInfoAndDeviceIdsVO;
import com.ruoyi.system.domain.dto.AirPlaneAndDeviceInfoAndTotalDeviceIdsVO;
import com.ruoyi.system.domain.dto.TaskUninstallDTO;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.ITbTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbDeviceApps;
import com.ruoyi.system.service.ITbDeviceAppsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备安装应用信息Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(tags = "设备安装应用信息管理")
@RestController
@RequestMapping("/system/apps")
public class TbDeviceAppsController extends BaseController {
    @Autowired
    private ITbDeviceAppsService tbDeviceAppsService;

    @Autowired
    private ITbTaskService tbTaskService;

    /**
     * 查询设备安装应用信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:apps:list')")
    @ApiOperation("获取设备安装应用信息列表")
    @GetMapping("/list")
    public R<List<TbDeviceApps>> list(TbDeviceApps tbDeviceApps) {
        //   startPage();
        List<TbDeviceApps> list = tbDeviceAppsService.selectTbDeviceAppsList(tbDeviceApps);
        // return getDataTable(list);
        return R.ok(list);
    }


    @ApiOperation("获取设备安装应用信息列表")
    @GetMapping("/page")
    public TableDataInfo page(TbDeviceApps tbDeviceApps) {
        startPage();
        List<TbDeviceApps> list = tbDeviceAppsService.selectTbDeviceAppsList(tbDeviceApps);
        return getDataTable(list);

    }


    /**
     * 导出设备安装应用信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:apps:export')")
    @ApiOperation("导出设备安装应用信息列表")
    @Log(title = "设备安装应用信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbDeviceApps tbDeviceApps) {
        List<TbDeviceApps> list = tbDeviceAppsService.selectTbDeviceAppsList(tbDeviceApps);
        ExcelUtil<TbDeviceApps> util = new ExcelUtil<TbDeviceApps>(TbDeviceApps.class);
        util.exportExcel(response, list, "设备安装应用信息数据");
    }

    /**
     * 获取设备安装应用信息详细信息
     */


    @ApiOperation("根据设备sn获取安装应用信息详细信息列表")
    //  @PreAuthorize("@ss.hasPermi('system:apps:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbDeviceAppsService.selectTbDeviceAppsById(id));
    }


    /**
     * 新增设备安装应用信息
     */
    @ApiOperation("新增设备安装应用信息")
    // @PreAuthorize("@ss.hasPermi('system:apps:add')")
    @Log(title = "设备安装应用信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbDeviceApps tbDeviceApps) {
        return toAjax(tbDeviceAppsService.insertTbDeviceApps(tbDeviceApps));
    }

    /**
     * 修改设备安装应用信息
     */
    //@PreAuthorize("@ss.hasPermi('system:apps:edit')")
    @ApiOperation("修改设备安装应用信息")
    @Log(title = "设备安装应用信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbDeviceApps tbDeviceApps) {
        return toAjax(tbDeviceAppsService.updateTbDeviceApps(tbDeviceApps));
    }

    /**
     * 删除设备安装应用信息
     */
    @ApiOperation("删除设备安装应用信息")
    //  @PreAuthorize("@ss.hasPermi('system:apps:remove')")
    @Log(title = "设备安装应用信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbDeviceAppsService.deleteTbDeviceAppsByIds(ids));
    }


//    @ApiOperation("应用下架/所有飞机/所有设备")
//    @PutMapping("/app/decommissioned/all/device")
//    public AjaxResult decommissioned(Long catalogId) {
//
//        return toAjax(tbDeviceAppsService.updateAppStatus(catalogId));
//    }

    @ApiOperation("查询全量下架受影响的飞机数/设备数/以及所需流量")
    @GetMapping("/get/app/decommissioned/all/info")
    public R<AviationAppStatsVO> getdecommissionedInfo(Long catalogId) {
        return R.ok(tbDeviceAppsService.getdecommissionedInfo(catalogId));
    }



    @ApiOperation("创建卸载应用任务")
    @PostMapping("/uninstall/task")
    public R decommissionedInfoCreateTask(@Valid @RequestBody TaskUninstallDTO dto) {
        return R.ok(tbTaskService.decommissionedInfoCreateTask(dto));
    }


//    @ApiOperation("应用下架/按飞机号选择下架")
//    @PostMapping("/app/decommissioned/device/byplaneCode")
//    public R decommissionedByPlaneCode(@RequestBody DecommissionedByPlaneCodeRequest request) {
//        return R.ok(tbDeviceAppsService.updateAppNameStatusByPlaneCodes(request));
//    }


    @ApiOperation("应用下架/查询安装了该应用飞机号/关联的设备数量")
    @GetMapping("/get/app/decommissioned/byappName/info")
    public R<List<AirPlaneAndDeviceInfoVO>> getDeviceAppInfoByAppName(Long catalogId) {
        return R.ok(tbDeviceAppsService.selectDeviceAppInfoByApp(catalogId));
    }


//    @ApiOperation("应用下架/按设备sn选择下架")
//    @PostMapping("/app/decommissioned/device/deviceCode")
//    public R decommissionedByDeviceCode(@RequestBody DecommissionedByPlaneCodeandDeviceInfoRequest request) {
//        return R.ok(tbDeviceAppsService.updateAppNameStatusByDeviceSn(request));
//    }


    @ApiOperation("应用下架/查询安装了该应用飞机号列表/关联的设备信息列表")
    @GetMapping("/get/app/decommissioned/byappName/AirPlane/device/info")
    public R < List<AirPlaneAndDeviceDetailInfoVO>> getDeviceAppAirplaneAndDeviceInfoByAppName
            (@RequestParam(required = true) Long catalogId
                    , @RequestParam(required = false) String deviceSn
                    , @RequestParam(required = false) String planeCode) {
        return R.ok(tbDeviceAppsService.getDeviceAppAirplaneAndDeviceInfoByAppName(catalogId, deviceSn, planeCode));
    }

}
